package com.zhentao.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zhentao.file.service.FileStorageService;
import com.zhentao.mapper.AdBannerMapper;
import com.zhentao.model.banner.dto.BannerDto;
import com.zhentao.model.banner.pojo.AdBanner;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.AdBannerService;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ad_banner】的数据库操作Service实现
* @createDate 2024-07-01 09:52:07
*/
@Service
public class AdBannerServiceImpl extends ServiceImpl<AdBannerMapper, AdBanner>
    implements AdBannerService{
    @Resource
    AdBannerMapper bannerMapper;
    @Resource
    FileStorageService fileStorageService;


    /*
     * 实现查询所有图片数据方法
     * */
    @Override
    public ResponseResult findAll(BannerDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<AdBanner> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<AdBanner> wrapper = new QueryWrapper<>();
        page = bannerMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    /*
     * 实现图片上传方法
     * */
    @Override
    public ResponseResult uploadPicture(AdBanner banner) {
       if (banner==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
       }
       bannerMapper.insert(banner);
       return ResponseResult.okResult("上传成功");
    }

    @Override
    public ResponseResult deleteByid(int id) {
        AdBanner banner = bannerMapper.selectById(id);
        if (banner==null){
            return ResponseResult.errorResult(400,"未找到banner");
        }
        banner.setIsDeleted(banner.getIsDeleted()==0 ? 1:0);
        bannerMapper.updateById(banner);
        return ResponseResult.okResult("状态成功");
    }

    @Override
    public ResponseResult findUpAll() {
        QueryWrapper<AdBanner> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AdBanner::getIsDeleted,0);
        List<AdBanner> adBanners = bannerMapper.selectList(wrapper);
        return ResponseResult.okResult(adBanners);
    }

}




